package com.lgx.common.web.json;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.lgx.common.web.Pagination;

public class Json2Utils {

	public static void parseJSON(Pagination page,HttpServletResponse response)
	{
		List<?> list = page.getRows();
		JSONObject json = new JSONObject();
		JSONArray jarr = new JSONArray();
		
		for(Object obj : list)
		{
			jarr.add(obj.toString());
		}
		
		json.put("total",page.getTotal());
		json.put("rows", jarr);
		
		PrintWriter out = null;
		try {
			response.setContentType( "application/json");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.write(json.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			if(out != null)
			out.close();
		}
	}
	
	public static void parseJSON(List<?> list,HttpServletResponse response)
	{
		JSONArray jarr = new JSONArray();
		
		for(Object obj : list)
		{
			jarr.add(obj.toString());
		}
		
		PrintWriter out = null;
		try {
			response.setContentType( "application/json");
			response.setCharacterEncoding("UTF-8");
			out = response.getWriter();
			out.write(jarr.toString());
			out.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}finally
		{
			if(out != null)
			out.close();
		}
	}
}
